import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

//import SwingWorkerProgress.TestPane;

public class SwingWorkerProgress {

	DataBase DB;
	TestPane tp = new TestPane();
	JFrame frame;
    public static void main(String[] args) {
   //     new SwingWorkerProgress();
    }

    public SwingWorkerProgress(DataBase DB) {
    	this.DB = DB;
        EventQueue.invokeLater(new Runnable() {
            

			@Override
            public void run() {
                
                	try {
						UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (UnsupportedLookAndFeelException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
               

              /*  frame = new JFrame("Progress");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                tp = new TestPane();
                frame.add(tp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);*/
            }
        });
    }

    public class TestPane extends JPanel {

        public JProgressBar pbProgress;
        public JButton start;
        public GridBagConstraints gbc;

        public TestPane() {

           
        	setBorder(new EmptyBorder(10, 10, 10, 10));
            pbProgress = new JProgressBar();
            
            setLayout(new GridBagLayout());
            gbc = new GridBagConstraints();
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(pbProgress, gbc);

            
         /*   start = new JButton("Start");
            gbc.gridy++;
            add(start, gbc);
         */
           /* start.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    start.setEnabled(false);
                    po();
                }

			
            });
            */

        }
    	public void po(final JProgressBar pb) {
			  ProgressWorker pw = new ProgressWorker();
              pw.addPropertyChangeListener(new PropertyChangeListener() {

                  @Override
                  public void propertyChange(PropertyChangeEvent evt) {
                      String name = evt.getPropertyName();
                      if (name.equals("progress")) {
                          int progress =  (Integer) evt.getNewValue();
                          pbProgress.setValue(progress);
                          repaint();
                      } else if (name.equals("state")) {
                          SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();
                          switch (state) {
                              case DONE:
                            	  pb.setVisible(false);
      			     	   		  JOptionPane.showMessageDialog(null,"DataBase build successfully","Finish",JOptionPane.INFORMATION_MESSAGE);

                                  //start.setEnabled(true);
                            	 // frame.setVisible(false);
                                  break;
                          }
                      }
                  }

              });
              pw.execute();					
		}
    }

    public class ProgressWorker extends SwingWorker<Object, Object> {

        @Override
        protected Object doInBackground() throws Exception {
          
       		DB.Start(this);

            return null;
        }
        
        public void setP(int progress)
        {
            setProgress(progress);

        }
    }
}
